package com.intelligence.pda.ui.abnormal.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.intelligence.pda.R;
import com.intelligence.pda.ui.abnormal.model.PhotographModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator
 * on 2017/10/23.
 */

public class PhotographAdapter extends BaseAdapter {
    private Context mActivity;
    private LayoutInflater mInflater = null;

    public PhotographAdapter(Activity activity) {
        this.mActivity = (Context) activity;
        this.mInflater = LayoutInflater.from(activity);
    }

    private List<PhotographModel> refundModels = new ArrayList<>();


    public void addData(List<PhotographModel> refundModels) {
        this.refundModels = refundModels;
        notifyDataSetChanged();
    }

    public void clearData() {
        this.refundModels.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return refundModels.size();
    }

    @Override
    public Object getItem(int position) {
        return refundModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotographModel prodectionModel = refundModels.get(position);
        ViewHolder holder = null;
        //如果缓存convertView为空，则需要创建View
        holder = new ViewHolder();
        //根据自定义的Item布局加载布局
        convertView = mInflater.inflate(R.layout.item_photograph, parent, false);
        holder.iv_Photograph = (ImageView) convertView.findViewById(R.id.iv_Photograph);
        holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
        convertView.setTag(holder);

        if (prodectionModel.getBitmap()==null) {
            holder.iv_Photograph.setImageResource(prodectionModel.getImg());
            holder.tv_name.setText(prodectionModel.getName());
        } else {
            holder.iv_Photograph.setImageBitmap(prodectionModel.getBitmap());
            holder.tv_name.setVisibility(View.GONE);
        }
        return convertView;

    }

    class ViewHolder {
        ImageView iv_Photograph;
        TextView tv_name;

    }

}